package test2.quartz;

import java.text.ParseException;
import java.util.HashSet;
import java.util.Set;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class App {
	
	public static void main(String[] args) throws ParseException {
		try {
			SchedulerFactory sf = new StdSchedulerFactory();
			Scheduler sched = sf.getScheduler();
			
			JobDetail job = JobBuilder.newJob(HelloJob.class)
					.withIdentity("myJob", "group1")
					.usingJobData("name", "zhangsan")
					.build();
			
			JobDetail job2 = JobBuilder.newJob(HelloJob.class)
					.withIdentity("myJob2", "group1")
					.usingJobData("name", "lisi")
					.build();
			
			Trigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("myTrigger", "group1")
					.startNow()
//					.startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2016-01-22 10:12"))
					.withPriority(10)
					.withSchedule(SimpleScheduleBuilder
							.simpleSchedule()
							.withIntervalInSeconds(5)
							.repeatForever())
					.build();
			
			Trigger trigger2 = TriggerBuilder.newTrigger()
					.withIdentity("myTrigger2", "group1")
					.startNow()
//					.startAt(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse("2016-01-22 10:12"))
					.withPriority(6)
//					优先级
					.withSchedule(SimpleScheduleBuilder
							.simpleSchedule()
							.withIntervalInSeconds(3)
							.repeatForever())
					.build();
			
			Set<Trigger> triggers = new HashSet<>();
			triggers.add(trigger);
			triggers.add(trigger2);
			
//			sched.getListenerManager().addJobListener(new JobListener() {
//				
//				@Override
//				public void jobWasExecuted(JobExecutionContext context, JobExecutionException jobException) {
//					//执行后
//					System.out.println("jobWasExecuted");
//				}
//				
//				@Override
//				public void jobToBeExecuted(JobExecutionContext context) {
//					//执行前
//					System.out.println("jobToBeExecuted");
//				}
//				
//				@Override
//				public void jobExecutionVetoed(JobExecutionContext context) {
//					//执行被拒绝
//					System.out.println("jobExecutionVetoed");
//				}
//				
//				@Override
//				public String getName() {
//					return "myListener";
//				}
//			});
			
//			schedulerListener
//			triggerListener
			
			sched.scheduleJob(job, trigger);
//			sched.scheduleJob(job, triggers, false);
//			sched.scheduleJob(job2, trigger2);
			
			sched.start();
			
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

}

